Borland Online And The Cobb Group Present:


May, 1994 - Vol. 1 No. 5

Borland C++ 4.0 Feature - Defining a custom style sheet for compiler options

In Setting compiler options by using a style sheet in last month's issue of Borland C++ Developer's Journal, we showed you how you can use style sheets with version 4.0 to set the compiler options for an entire project or individual source file. In that article, we demonstrated the technique by applying one of the existing style sheets that come with Borland C++ 4.0.

However, you can define your own style sheets to apply special combinations of compiler options to a project or source file. In this article, we'll show you how to create a new style sheet for a project.

User style sheets

To create a new style sheet, you'll open the Style Sheet dialog box. From this dialog box, you'll first specify the name of the new style sheet.

After you name the style sheet, you'll set each of the compiler options for the style sheet from the Style Sheet dialog box. Once you've finished setting the style sheet's compiler options, you'll close this dialog box to save the style sheet's settings. The style sheet will then be ready for you to use.

As an example, let's create a simple style sheet that prevents the compiler from accepting code that tries to use Exception Handling. Since many compilers still don't support Exception Handling, you may want to use this style sheet (or a composite style sheet based on this one) for projects you want to support on other compilers.

Creating the new style sheet

To begin, launch the Borland C++ version 4.0 Integrated Development Environment (IDE). When the IDE desktop appears, choose Style Sheets... from the Options menu.

In the Style Sheets dialog box, click the Create... button. When the Create Style Sheet dialog box appears, enter No Exceptions as the name of your new style sheet, as shown in Figure A, and click OK.


Figure A - You specify the name of the new style sheet in the Create Style Sheet dialog box.

When the name No Exceptions appears in the Available Style Sheets list box, double-click on the name. When the Style Sheet: No Exceptions dialog box appears, click on the plus sign (+) that appears next to the name C++ Options in the Topics list box.

In the list of subtopics that appears beneath the name C++ Options, select Exception Handling/RTTI. In the Exception Handling/RTTI section of the dialog box, deselect Enable Exceptions and Enable Destructor Cleanup. When you finish, the Style Sheet: No Exceptions dialog box should resemble the one shown in Figure B.

Figure B - You use the Style Sheet dialog box to set the compiler options for each style sheet.

Click OK to save the No Exceptions style sheet. When the Style Sheets dialog box reappears, click Close to return to the IDE.

Applying the style sheet

Now let's see how we can use this style sheet with a very simple program. In this program, we'll use the basic Exception Handling structure with no real functionality. When we use the No Exceptions style sheet to compile the program, the compiler will generate the appropriate error.

To create this project, choose New Project... from the Project menu. When the New Project dialog box appears, you enter

\BC4\NOEX\NOEX.IDE

in the dialog box's Project Path and Name entry field.

In the Platform combo box, choose DOS Standard as the application type. Create the project by clicking OK.

When the NOEX.IDE Project window appears, double-click on the filename noex[.cpp]. When the file's editor window appears, enter the code from Listing A.

When you finish entering the source code, bring the Project window to the front. Click on the name noex[.exe] with the right mouse button. In the pop-up menu that appears, choose Edit Node Attributes....

When the Node Attributes For NOEX dialog box appears, select No Exceptions from the Style Sheet combo box, as shown in Figure C. Click OK to save this setting.


Figure C - You apply a style sheet to a project from the Node Attributes dialog box.


Listing A: NOEX.CPP
int main()
{
  try
  {
    throw("exception");
  }

  catch(const char*)
  {
  }
  return 0;
}

Double-click on noex[.exe] in the Project window to begin compiling the project. As the compiler runs, the Compile Status window will show one error and one warning for NOEX.CPP.

Click the OK button in the Compile Status dialog box. When the Message window appears, it will show that the source file tries to use Exception Handling without enabling that feature, as shown in Figure D.


Figure D - If you try to use Exception Handling in a project that uses the No Exceptions style sheet, you'll see this error message.

Choose Close Project from the Project menu. To exit the IDE, choose Exit from the File menu.

Conclusion

Style sheets are an easy way for you to set common compiler options for a project or source file. By defining your own style sheets, you can quickly apply compiler settings that are appropriate for your projects.

Return to the Borland C++ Developer's Journal index

Subscribe to the Borland C++ Developer's Journal


Copyright (c) 1996 The Cobb Group, a division of Ziff-Davis Publishing Company. All rights reserved. Reproduction in whole or in part in any form or medium without express written permission of Ziff-Davis Publishing Company is prohibited. The Cobb Group and The Cobb Group logo are trademarks of Ziff-Davis Publishing Company.